{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0aa38f0f",
   "metadata": {},
   "source": [
    "# 3.2 循环语句\n",
    "## 3.2.1 while循环(有else)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2cba096a",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (3600606221.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[10], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m    while <test>:\t\t#Loop test\u001b[0m\n\u001b[1;37m          ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "while <test>:\n",
    "    <statements1>\n",
    "else:\n",
    "    <statements2>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c13bda5c",
   "metadata": {},
   "source": [
    "## 3.2.2 for循环"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92d2190f",
   "metadata": {},
   "source": [
    "* for循环在Python中是一个通用的序列迭代器：可以遍历任何有序的序列对象内的元素。\n",
    "* 循环的嵌套是指在一个循环中又包含另外一个完整的循环，即循环体中又包含循环语句。while循环和for循环可以相互嵌套。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ec7175ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "没有偶数\n"
     ]
    }
   ],
   "source": [
    "a=[1,3,5,7,9]\n",
    "for i in a:\n",
    "    if i % 2 == 0:\n",
    "        break\n",
    "else:\n",
    "    print(\"没有偶数\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9274baee",
   "metadata": {},
   "source": [
    "#### range(start,end,step)左闭右开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7194d892",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for x in range(10):\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e8301cff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "6\n",
      "11\n",
      "16\n"
     ]
    }
   ],
   "source": [
    "for x in range(1,20,5):\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17904d9b",
   "metadata": {},
   "source": [
    "#### enumerate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d56d1f92",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:1\n",
      "1:3\n",
      "2:5\n",
      "3:7\n",
      "4:9\n"
     ]
    }
   ],
   "source": [
    "for i,x in enumerate(a):\n",
    "    print(f\"{i}:{x}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cd23251b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<enumerate at 0x202d8ab1440>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "enumerate(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "15846364",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(0, 1), (1, 3), (2, 5), (3, 7), (4, 9)]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(enumerate(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5c7734c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x,y,z = a[:3]#将a的前三项赋给x，y，z\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f720990",
   "metadata": {},
   "source": [
    "## 3.2.3 break continue pass\n",
    "* break结束当前循环\n",
    "* continue 忽略下面的语句，进入下一次循环\n",
    "* pass相当于空语句，因为Python不允许空语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d1fe61fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "if a:\n",
    "    pass #相当于一个空语句"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6acbb1f4",
   "metadata": {},
   "source": [
    "## 3.2.4 try-except "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "214e723a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter name:a\n",
      "Enter age:19\n",
      "age: 19\n"
     ]
    }
   ],
   "source": [
    "while 1:\n",
    "    name = input(\"Enter name:\")\n",
    "    if name == 'stop':\n",
    "        break\n",
    "    age = input(\"Enter age:\")\n",
    "    print(\"age:\",age)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1bc756c",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    i=int('a12')\n",
    "except:\n",
    "    print(\"Value error\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e78d1e39",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = [1, 2, 3]  # 定义一个列表 m，包含元素 1, 2, 3\n",
    "b = [x**2 for x in m]  # 使用列表推导式，对 m 中的每个元素 x 进行平方操作，并将结果存储在列表 b 中\n",
    "print(b)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
